package com.tianbing.adlottery.controller.hordeadmin;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.User2Content;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class UploadMyServedProductImagesController extends SimpleFormController {
	static Logger logger = Logger.getLogger(UploadMyServedProductImagesController.class.getName());

	private IUserDao userDao;

	private ISiteDao siteDao;

	private IUserBusiness userBusiness;

	private SkinUtils skinUtils;

	private String fileUploadDir = "";

	private Integer logoImageMaxSize = 10240;// 10k

	private Integer smallImageMaxSize = 102400;// 100k

	private Integer mediumImageMaxSize = 512000;// 500k

	private Integer largeImageMaxSize = 1024000;// 1M

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		User2Content s2c = userDao.findUser2ContentBy(u2csid);
		fb.setUser2Content(s2c);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?u2csid=" + u2csid);
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);

		if (form.getLogoImage() != null) {
			MultipartFile f = form.getLogoImage();
			if (0 < f.getSize() && f.getSize() <= logoImageMaxSize) {
				String logoImageFileName = fileUploadDir + csid + "logo.jpg";
				FileOutputStream fos = new FileOutputStream(logoImageFileName);
				FileCopyUtils.copy(f.getInputStream(), fos);
				fos.close();
			} else if (f.getSize() > logoImageMaxSize) {
				mv.getModel().put("tips", "Logo file size exceed 10k.");
			}
		}
		if (form.getSmallImage() != null) {
			MultipartFile f = form.getSmallImage();
			if (0 < f.getSize() && f.getSize() <= smallImageMaxSize) {
				String smallImageFileName = fileUploadDir + csid + "small.jpg";
				FileOutputStream fos = new FileOutputStream(smallImageFileName);
				FileCopyUtils.copy(f.getInputStream(), fos);
				fos.close();
			} else if (f.getSize() > smallImageMaxSize) {
				mv.getModel().put("tips", "Logo file size exceed 10k.");
			}
		}
		if (form.getMediumImage() != null) {
			MultipartFile f = form.getMediumImage();
			if (0 < f.getSize() && f.getSize() <= mediumImageMaxSize) {
				String mediumImageFileName = fileUploadDir + csid + "medium.jpg";
				FileOutputStream fos = new FileOutputStream(mediumImageFileName);
				FileCopyUtils.copy(f.getInputStream(), fos);
				fos.close();
			} else if (f.getSize() > mediumImageMaxSize) {
				mv.getModel().put("tips", "Logo file size exceed 10k.");
			}
		}
		if (form.getLargeImage() != null) {
			MultipartFile f = form.getLargeImage();
			if (0 < f.getSize() && f.getSize() <= largeImageMaxSize) {
				String largeImageFileName = fileUploadDir + csid + "large.jpg";
				FileOutputStream fos = new FileOutputStream(largeImageFileName);
				FileCopyUtils.copy(f.getInputStream(), fos);
				fos.close();
			} else if (f.getSize() > largeImageMaxSize) {
				mv.getModel().put("tips", "Logo file size exceed 10k.");
			}
		}

		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		// List user2Categories =
		// userDao.getListUser2CategoryBy(authorization.getUserSysId(),
		// User2Category.RelationType.favorite, User2Category.Status.approve,
		// -1L,null, -1, -1, null);
		// // .getUserGroupsBy(authorization.getUserSysId(),
		// // User2Category.RelationType.favorite);
		// List categoryTrails = new ArrayList(0);
		// for (int i = 0; user2Categories != null && i <
		// user2Categories.size(); i++) {
		// User2Category pg = (User2Category) user2Categories.get(i);
		// List categoryTrail =
		// categoryDao.getListCategoryPrimaryTrailBy(pg.getCategory().getSysId());
		// categoryTrails.add(categoryTrail);
		// }
		// if (categoryTrails.size() > 0) {
		// model.put("categoryTrails", categoryTrails);
		// }
		//
		// String ptgid = ServletRequestUtils
		// .getStringParameter(request, "ptgid",
		// Global.CategoryID.place.getCategoryId());
		// List categoryTrail =
		// categoryDao.getListCategoryPrimaryTrailBy(ptgid);
		// List partySubGroups = categoryDao.getListChildCategoryBy(ptgid);
		// model.put("categoryTrail", categoryTrail);
		// model.put("partySubGroups", partySubGroups);
		return model;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	private boolean isDemoUser(UserSession authorization) {
		if (authorization != null && authorization.getUserId() != null
				&& authorization.getUserId().equals("demo@500hao.com"))
			return true;
		return false;
	}

	public static void setLogger(Logger logger) {
		UploadMyServedProductImagesController.logger = logger;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setFileUploadDir(String fileUploadDir) {
		this.fileUploadDir = fileUploadDir;
	}

	public void setLargeImageMaxSize(Integer largeImageMaxSize) {
		this.largeImageMaxSize = largeImageMaxSize;
	}

	public void setLogoImageMaxSize(Integer logoImageMaxSize) {
		this.logoImageMaxSize = logoImageMaxSize;
	}

	public void setMediumImageMaxSize(Integer mediumImageMaxSize) {
		this.mediumImageMaxSize = mediumImageMaxSize;
	}

	public void setSmallImageMaxSize(Integer smallImageMaxSize) {
		this.smallImageMaxSize = smallImageMaxSize;
	}
}
